app.controller("homePluginCtrl", function ($scope, $rootScope, $http) {
    $rootScope.activeSidebar = Class.getConstant('SIDEBAR_PLUGIN');

    $scope.isEdit = false;

    getPluginMsg($rootScope, $scope, $http);

    $scope.editFile = function (fileUrl, fileName) {
        $scope.isEdit = true;
        $scope.pluginName = fileName;
        $scope.fileUrl = fileUrl;
    };
    $scope.uploadPlugin = function () {
        console.log("uploadPlugin");
        $scope.isEdit = false;

        var url = $rootScope.rootUrl + Http.UpdatePluginMsg;//接口路径
        var fd = new FormData();
        // var file = document.querySelector('input[type=file]').files[0];
        if ($scope.file) {
            console.log($scope.file[0]);
        } else {
            alert('$scope.file 为空！');
            return;
        }
        fd.append($scope.file[0].name, $scope.file[0]);//参数 testReport=后台定义上传字段名称 ； $scope.file[0] 内容
        $http.post(url, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }).success(function (data) {
            console.log(data);
            if (data.status != 1000) {
                alert(JSON.stringify('文件导入失败：' + $scope.file[0].name + '，请重新上传正确的文件或格式'));
            } else {
                getPluginMsg($rootScope, $scope, $http);
                alert(JSON.stringify('文件导入成功：' + $scope.file[0].name));
            }
            //恢复按钮
            $scope.isEdit = false;
        }).error(function (data) {
            alert('服务器错误，文件导入失败！');
            //恢复按钮
            $scope.isEdit = false;
        });
    };
    $scope.deletePlugin = function () {
        $scope.isEdit = false;
        var url = $rootScope.rootUrl + Http.DeletePluginByName;
        var jsonobj = {Name: $scope.pluginName};
        $http.post(url, jsonobj).success(function (data) {
            console.log(data);
            if (data.status == 1000) {
                getPluginMsg($rootScope, $scope, $http);
                alert('文件删除成功');
            } else {
                alert('文件删除失败,' + data.desc);
            }
        }).error(function (data) {
            alert('服务器错误，文件删除失败！');
        });
        $scope.pluginName = "";
    };
});
//文件上传
app.directive("pluginUpload", function () {
    return {
        //通过设置项来定义
        restrict: 'AE',
        scope: false,
        template: '<div class="fl"><input type="button" id="storeBtn" style="padding:0; position: absolute; top: 0; left: 0; background: none; border: none;color: #fff; width:84px; height: 34px; line-height: 34px;" value="选择文件"><input type="file" name="testReport" id="file" ng-disabled="isEdit" style="position: absolute; top: 0; left: 0; opacity: 0; width:84px; height: 34px;" accept=".jpg,.png,.apk,.jar,.war,.rar,.zip"></div>', //name:testReport 与接口字段相对应。
        replace: true,
        link: function (scope, ele, attrs) {
            ele.bind('click', function () {
                console.log("fileUpload click");
                $('#file').val('');
            });
            ele.bind('change', function () {
                console.log("fileUpload change");
                scope.file = ele[0].children[1].files;
                if (scope.file[0].size > 52428800) {
                    alert("大小不大于50M");
                    scope.file = null;
                    return false;
                }
                scope.pluginName = scope.file[0].name;
                var postfix = scope.pluginName.substring(scope.pluginName.lastIndexOf(".") + 1).toLowerCase();
                // if (postfix != "jpg" && postfix != "png") {
                //     alert("图片仅支持png、jpg类型的文件");
                //     scope.fileName = "";
                //     scope.file = null;
                //     scope.$apply();
                //     return false;
                // }
                scope.$apply();
                scope.reader = new FileReader(); //创建一个FileReader接口
                console.log(scope.reader);
                console.log(scope.file);
                if (scope.file) {
                    //获取图片（预览图片）
                    scope.reader.readAsDataURL(scope.file[0]); //FileReader的方法，把图片转成base64
                    scope.reader.onload = function (ev) {
                        scope.$apply(function () {
                            scope.thumb = {
                                imgSrc: ev.target.result  //接收base64，scope.thumb.imgSrc为图片。
                            };
                        });
                    };
                } else {
                    alert('上传不能为空!');
                }
            });
        }
    };
});

function getPluginMsg($rootScope, $scope, $http) {
    var url = $rootScope.rootUrl + Http.GetPluginMsg;
    $http({method: "POST", url: url}).then(function (data) {
        console.log(data);
        $scope.pluginList = data.data.resultMap.data;
    });
}